package com.headline.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.headline.pojo.User;
import com.headline.service.UserService;
import com.headline.mapper.UserMapper;
import com.headline.utils.JwtHelper;
import com.headline.utils.MD5Util;
import com.headline.utils.Result;
import com.headline.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author 断念残情
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-04-11 17:20:21
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtHelper jwtHelper;
    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据账号查询数据库
        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        User loginUser = userMapper.selectOne(lambdaQueryWrapper);
        if(loginUser == null){
            return  Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //如果账户名没有问题就对比密码
        if(!StringUtils.isEmpty(user.getUserPwd())
                && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())){
            //满足则登陆成功，并根据用户id生成token
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            Map map = new HashMap();
            map.put("token",token);
            //将token封装返回
            return Result.OK(map);
        }
        //否则登陆失败,密码错误
        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
    }

    /**
     * 从token中获取到用户信息，并响应给客户端存储
     * @param token
     * @return
     */
    @Override
    public Result getUserInfo(String token) {
        //校验token是否有效
        boolean expiration = jwtHelper.isExpiration(token);
        //如果expiration为true 则过期了，返回过期消息，如果为false，说明还没有过期
        if(expiration){
            //失效未登录
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        int userId = jwtHelper.getUserId(token).intValue();
        //根据id查询用户
        User user = userMapper.selectById(userId);
        user.setUserPwd("");
        Map map = new HashMap();
        map.put("loginUser",user);

        return Result.OK(map);
    }

    /**
     * 检查用户名是否被占用
     * @param username
     * @return
     */
    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername,username);

        User user = userMapper.selectOne(lambdaQueryWrapper);
        if(user == null){
            return Result.OK(null);
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }

    @Override
    public Result regist(User user) {
        //先检查用户名是否被占用
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        Long count = userMapper.selectCount(lambdaQueryWrapper);

        if(count == 0){
            //如果count == 0 ,说明数据库中没有这个用户，可以注册
            String encrypt = MD5Util.encrypt(user.getUserPwd());
            user.setUserPwd(encrypt);
            userMapper.insert(user);
            return Result.OK(null);
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }


}




